<template>
  <el-dialog
    :visible.sync="dialogVisible"
    width="390px"
    :before-close="handleClose"
  >
    <div class="content">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="70px"
      >
        <div class="title">忘记密码</div>
        <el-form-item label="手机号/邮箱" prop="mobile">
          <el-input
            v-model="ruleForm.mobile"
            placeholder="请输入手机号/邮箱”"
          ></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="code">
          <el-input
            v-model="ruleForm.code"
            placeholder="请输入验证码"
          ></el-input>
          <sendCode :mobile="ruleForm.mobile" event="resetpwd"></sendCode>
        </el-form-item>
        <el-form-item label="新密码" prop="password">
          <el-input
            v-model="ruleForm.password"
            placeholder="请输入密码"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="repassword">
          <el-input
            v-model="ruleForm.repassword"
            placeholder="请再次输入密码"
            show-password
          ></el-input>
        </el-form-item>
        <div class="btn pointer" @click="onSubmit">确认找回</div>
        <div class="bottom">
          <span class="pointer" @click="handleLogin">想起密码? 去登录</span>
        </div>
      </el-form>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: "loginView",
  data() {
    return {
      dialogVisible: false,
      ruleForm: {
        mobile: "",
        password: "",
      },
      rules: {
        // tel: [{ message: "请输入手机号码", trigger: "blur" }],
        // password: [{ message: "请输入密码", trigger: "blur" }],
      },
      img: require("@/assets/login.png"),
    };
  },
  components: {
    sendCode: () => import("@/components/popup/sendCode.vue"),
  },
  props: {
    visible: {
      default: false,
    },
  },
  watch: {
    visible(newVal) {
      this.dialogVisible = newVal;
    },
  },
  methods: {
    handleClose() {
      this.dialogVisible = false;
      this.$emit("update:visible", false);
    },
    handleLogin() {
      this.handleClose();
      this.$emit("handleLogin", true);
    },
    onSubmit() {
      if (this.ruleForm.password !== this.ruleForm.repassword) {
        this.$message.error("两次密码输入不一致");
        return false;
      }
      this.$post("/api/user/resetpwd", this.ruleForm).then((res) => {
        if (res.code == 1) {
          this.$message.success(res.msg);
          this.handleClose();
        } else {
          // this.$message.error(res.msg);
        }
      });
    },
  },
};
</script>

<style scoped lang="scss">
::v-deep .el-dialog__body {
  padding: 0;
}
::v-deep .el-dialog__header {
  padding: 0;
}
.content {
  background: #fff;
}
::v-deep .el-form {
  padding: 30px 35px;
  .title {
    text-align: center;
    font-size: 24px;
    padding-bottom: 20px;
  }
  .el-cascader {
    width: 100%;
  }
  .btn {
    text-align: center;
    color: #fff;
    background: #b08c45;
    border-radius: 4px;
    line-height: 40px;
    margin-bottom: 20px;
  }
  .bottom {
    display: flex;
    justify-content: space-between;
  }
}
@media screen and (max-width: 768px) {
  :deep .el-dialog {
    width: 90% !important;
  }
}
</style>
